package com.example.service;

import com.example.dao.DiscussDao;
import com.example.model.pojo.Discussion;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service
public class DiscussService implements IDiscussService {
    @Autowired
    private DiscussDao discussDao;
    @Override
    public Page<Discussion> loadAll(Integer pageNum, Integer pageSize) {
        Pageable pageable= PageRequest.of(pageNum,pageSize);
        Page<Discussion> page= discussDao.findAll(pageable);
        return page;
    }

    @Override
    public Page<Discussion> loadHotDiscussService(Integer pageNum, Integer pageSize) {
        Sort sort=Sort.by(new Sort.Order(Sort.Direction.DESC,"likeNum"),new Sort.Order(Sort.Direction.DESC,"viewNum"));
        Pageable pageable= PageRequest.of(pageNum,pageSize,sort);
        Page<Discussion> page= discussDao.findAll(pageable);
        return page;
    }

    @Override
    public Page<Discussion> loadNewDiscussService(Integer pageNum, Integer pageSize) {
        Sort sort=Sort.by(new Sort.Order(Sort.Direction.DESC,"gmtCreate"),new Sort.Order(Sort.Direction.DESC,"gmtModified"));
        Pageable pageable= PageRequest.of(pageNum,pageSize,sort);
        Page<Discussion> page= discussDao.findAll(pageable);
        return page;
    }

    @Override
    public int getDiscussSize() {
        return discussDao.countBy();
    }


}
